import './test.css'
const selectedClassName = 'current'
import Turndown from 'turndown'
import { gfm } from 'turndown-plugin-gfm'
import saveAs from 'file-saver'
const turndownService = new Turndown()
turndownService.use(gfm)
let isSelected = false
let outerHTML = null
export default defineContentScript({
    matches: ['<all_urls>'],
    main() {
        document.addEventListener('mousemove', (event) => {
            if (!isSelected) {
                event.target.classList.add(selectedClassName)
            }
        })
        document.addEventListener('mouseout', (event) => {
            if (!isSelected) {
                console.log('当鼠标移出元素时，移除元素的类')
                event.target.classList.remove(selectedClassName)
            }
        })
        document.addEventListener('click', (event) => {
            outerHTML = event.target
            isSelected = !isSelected
            //chrome.runtime.sendMessage({})
        })
        chrome.runtime.onMessage.addListener(function (data) {
            console.log(data)
            if (isSelected) {
                var markdown = turndownService.turndown(outerHTML)
                console.log(markdown)
                let blob = new Blob([markdown])
                saveAs(blob, 'bit.markdown')
            }
        })
    }
})
